<?php

/**
 * Well Framework
 * 
 * Contains Front Controller pattern class
 * 
 * @package     Well
 *
 * @author Valerii Lazarevych <valeriy.fremar@gmail.com>
 */

/**
 * FrontController class
 */
class FrontController
{
    /**
     * Contain application helper object
     * 
     * @var type applicationHelper 
     */
    protected $appHepler    =   null;         
    
    /**
     * Base url
     * @var string  $base_url 
     */
    public $_baseUrl     =   null;
    
    /**
     * Base file path
     * @var string
     */
    protected $_basePath    =   null;
    
    /**
     * Use it in late time
     * Mapping view to command
     * 
     * @var Dispatcher $_dispatcher 
     */
    protected $_dispatcher  =   null;
    
    /**
     * Logged user object
     * @var Accounts_domainObjects
     */
    public static $_loggedUser  =   null;
    
    /**
     * FrontController instance with singleton pattern
     * @var FrontController
     */
    protected static $_instance     =   null;

    
    /**
     * Private Constructur empty
     * @see run
     */
    private function __construct() {}
    
    
    /**
     * Get instance of web application - singleton
     * @return type 
     */
    public static function instance()
    {
        if (!self::$_instance instanceof self){            
             self::$_instance = new self();
        }        
        return self::$_instance;
    }  
    
    /**
     * Run web application
     * 
     * Get inbox request
     * Request Handling 
     * Execute its command
     * Send request response
     * 
     * @static     
     */
    static public function run()
    {        
        $instance   =   self::instance();
        
        //app initialize
        $instance->init();
        
        //request handling
        $instance->handleRequest();
                
        //sending response                
        $instance->requestResponse();
    }
    
    /**
     * Initialize application helper
     */
    private function init()
    {
       $this->appHepler =  ApplicationHelper::instance();       
       $this->appHepler->init();              
    }
    
    /**
     * Proccess request:
     * Get request
     * Get request command
     * Execute command.
     */
    private function handleRequest()
    {
        $request    =   ApplicationRegistry::get('request');
        
        $cmd_r      =   new CommandResolver();
        $cmd        =   $cmd_r->getCommand( $request );               
        
        $cmd->execute( $request );                
    }
    
    /**
     * Send response to inbox request
     * 
     * @todo rebuild it with Response object
     *      
     * Render Layout
     */
    private function requestResponse()
    {
        // Render response page get it        
        ApplicationRegistry::get('layout')->render();        
    }
    
    public function setBasePath($path)
    {
        $this->_basePath    =   $path;
    }
    
    public function getBasePath()
    {
        return $this->_basePath;
    }
}
?>
